package com.craftcomputing.client.Listeners;

import asquare.gwt.tk.client.ui.ModalDialog;
import asquare.gwt.tk.client.ui.RowPanel;

import com.craftcomputing.client.Entry.PhotoWidget.*;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.Window;

public class PhotoWidgetListeners {
	static public class PhotoWidgetLoadListener implements LoadListener{
		private Image im;
		public PhotoWidgetLoadListener(Image im){
			this.im = im;
		}
		public void onLoad(Widget wig){
			//Image img = (Image)wig;
			//Window.alert("finished loading: " + im.getUrl() + "(" + Integer.toString(im.getWidth()) + ", " + Integer.toString(im.getHeight()));
			try{
				int w = im.getWidth();
				int h = im.getHeight();
				float asp = w/(float)h;
				if(h <= 0) asp = 1;
				
				int newH = 240; 
				int newW = 320;
				if (asp < 4./3.){
					newH = 240;
					newW = (int)((float)newH*asp);			
				}
				else if(asp > 4./3.){
					newW = 320;
					newH = (int)((float)newW/asp);
				}
				String wStr = Integer.toString(newW) + "px";
				String hStr = Integer.toString(newH) + "px";
				im.setSize(wStr, hStr);
			}catch(Exception e){Window.alert("problem loading: " + im.getUrl());}
		}
		public void onError(Widget w){
			im.setUrl("mainimages/01_11a.jpg");
		}
	}
	static public void setImageSize(Image im){
		//Window.alert("finished loading: " + im.getUrl() + "(" + Integer.toString(im.getWidth()) + ", " + Integer.toString(im.getHeight()));
		int w = im.getWidth();
		int h = im.getHeight();
		float asp = w/(float)h;
		if(h <= 0) asp = 1;
		
		int newH = 240; 
		int newW = 320;
		if (asp < 4./3.){
			newH = 240;
			newW = (int)((float)newH*asp);			
		}
		else if(asp > 4./3.){
			newW = 320;
			newH = (int)((float)newW/asp);
		}
		String wStr = Integer.toString(newW) + "px";
		String hStr = Integer.toString(newH) + "px";
		im.setSize(wStr, hStr);
	}

	static public class PhotoWidgetMouseListener implements MouseListener{
		private Label label;
		private int id;
		private PhotoWidget photoWidget;
		public PhotoWidgetMouseListener(PhotoWidget photoWidget, Label label, int id){
			this.photoWidget = photoWidget;
			this.label = label;
			this.id = id;
		}
		public void onMouseDown(final Widget sender, final int x, final int y){
			photoWidget.setCurrent(id);
		}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseEnter(final Widget sender) {
			label.setStyleName("gwt-PhotoViewerButton-over");
			photoWidget.setImage(id);
		}
		public void onMouseLeave(final Widget sender) {
			if(photoWidget.getCurrent() != id)
				label.setStyleName("gwt-PhotoViewerButton");
				photoWidget.setImage(photoWidget.getCurrent());
		}
	}
	public static class PhotoAlbumCloseListener implements ClickListener
	{
		private final ModalDialog m_dialog;			
		public PhotoAlbumCloseListener(ModalDialog dialog){m_dialog = dialog;}
		public void onClick(Widget sender){m_dialog.hide();}
	}		
	public static class PhotoAlbumCloseButton extends Button
	{
		public PhotoAlbumCloseButton(ModalDialog dialog)
		{
			super("Close");
			addClickListener(new PhotoAlbumCloseListener(dialog));
		}
	}
	static public class PhotoMouseListener implements MouseListener{
		private Image im;
		public PhotoMouseListener(Image im){
			this.im = im;
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {
			final ModalDialog dialog = new ModalDialog();
			final RowPanel rp = new RowPanel();
			dialog.setCaption("Caption area", false);
			rp.add(new Image(im.getUrl()));
			rp.add(new PhotoAlbumCloseButton(dialog));
			dialog.add(rp);
			dialog.show();			
		}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseEnter(final Widget sender) {
			im.setStyleName("gwt-Photo-over");
		}
		public void onMouseLeave(final Widget sender) {
			im.setStyleName("gwt-Photo");
		}
	}
}
